#include "widget.h"

#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(600, 600);

    // 布局设置
    QPushButton* btnAdd = new QPushButton("添加");
    QPushButton* btnDel = new QPushButton("删除");
    QPushButton* btnLine = new QPushButton("--");

    QVBoxLayout* mainLayout = new QVBoxLayout();
    QHBoxLayout* upLayout = new QHBoxLayout(); // 图工具区

    // 设置图显示区
    scene = new QWidget();
    QPalette p = scene->palette();
    p.setColor(QPalette::Background, Qt::white);
    scene->setAutoFillBackground(true);
    scene->setPalette(p);

    // 设置界面布局
    upLayout->addWidget(btnAdd);
    upLayout->addWidget(btnDel);
    upLayout->addWidget(btnLine);

    mainLayout->addLayout(upLayout);
    mainLayout->addWidget(scene);

    this->setLayout(mainLayout);

    // 添加事件
    connect(btnAdd, SIGNAL(clicked()), this, SLOT(slotAddGraphic())); // 添加圆
    connect(btnDel, SIGNAL(clicked()), this, SLOT(slotDeleteEllipse())); // 删除圆
    connect(btnLine, SIGNAL(clicked()), this, SLOT(slotAddEdge())); // 添加无向图的边
}

void Widget::slotEllipseMove(QVector<int> indexs)
{
    for (int i = 0; i < indexs.size(); i++) {
        lines[indexs[i]]->resizeMyWidget();
    }
}

void Widget::slotAddGraphic()
{
    MyGraphicEllipse* ellipse = new MyGraphicEllipse;
    ellipse->move(100, 100);
    ellipse->setParent(this->scene);
    ellipse->show();

    // 圆的移动事件
    connect(ellipse, SIGNAL(signalMove(QVector<int>)), this, SLOT(slotEllipseMove(QVector<int>)));

    // 圆的选中事件
    connect(ellipse, SIGNAL(signalSelectChange(MyGraphicEllipse*, bool)),
            this, SLOT(slotEllipseSelectChange(MyGraphicEllipse*, bool)));
}

void Widget::slotEllipseSelectChange(MyGraphicEllipse *ellipse, bool select)
{
    if (select) {
        // 圆被选中，则添加到选中圆列表中
        selectEllipses.append(ellipse);
    } else {
        // 如果该圆没有被选中，则从选中圆列表中移除
        for (int i = 0; i < selectEllipses.size(); i++) {
            if (selectEllipses[i] == ellipse) {
                selectEllipses.removeAt(i);
                break;
            }
        }
    }

    qDebug() << "selectEllipses:" << selectEllipses;
}

void Widget::slotDeleteEllipse()
{
    QSet<int> deleteIndexs; // 存放要删除线的索引

    // 删除圆对象
    for (int i = 0; i < selectEllipses.size(); i++) {

        // 将所有线索引添加到删除索引集合中
        QVector<int> indexs = selectEllipses[i]->getLineIndex();
        for (int i = 0; i < indexs.size(); i++) {
            deleteIndexs.insert(indexs[i]);
        }

        // 删除圆对象
        delete selectEllipses[i];
    }

    // 删除线对象
    for (QSet<int>::iterator iter = deleteIndexs.begin(); iter != deleteIndexs.end(); iter++) {
        // 修改线连接的圆中存放的线索引
        lines[*iter]->removeEllipseLineIndex(*iter);

        // 删除线对象
        delete lines[*iter];

        // 避免影响索引位置
        lines[*iter] = nullptr;
    }

    // 删除空值
    lines.removeAll(nullptr);

    selectEllipses.clear();

    qDebug() << "lines:" << lines;
}

void Widget::slotAddEdge()
{
    if (selectEllipses.size() == 2) { // 必须有两个圆被选中
        MyGraphicLine* line = new MyGraphicLine;
        line->setParent(this->scene);
        line->show();

        // 线设置圆
        line->setEllipse1(selectEllipses[0]);
        line->setEllipse2(selectEllipses[1]);

        // 将线添加到集合
        lines.append(line);

        // 添加线信息到圆
        selectEllipses[0]->addLineIndex(lines.size() - 1);
        selectEllipses[1]->addLineIndex(lines.size() - 1);

        // 更新线数据
        line->resizeMyWidget();

        // 取消圆选中状态
        selectEllipses[0]->setSelect(false);
        selectEllipses[1]->setSelect(false);

        // 清空选中列表
        selectEllipses.clear();
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    // 当鼠标点击界面时，拿走所有焦距，让输入框不能再输入
    this->setFocus();
}

Widget::~Widget()
{
}

